//+------------------------------------------------------------------+
//|                                                 SpreadTwoCcy.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright"
#property link "yeloparis@yahoo.co.uk" 
#property indicator_separate_window  
#property indicator_buffers 4
#property indicator_color1 Coral
#property indicator_color2 Snow
#property indicator_color3 Snow 
#property indicator_color4 SpringGreen//Tomato 
#property indicator_style1 STYLE_SOLID 
#property indicator_style2 STYLE_DASHDOTDOT
#property indicator_style3 STYLE_DASHDOTDOT
#property indicator_style4 STYLE_DOT
#property indicator_width1 2 
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1  
//---- Currencies-------------------------------------------------------
extern string Synbol     = "EURUSD"; 
extern string SynbolCorr = "EURGBP";   
//---- indicator parameters---------------------------------------------
extern int    BandsPeriod=10;
extern int    MethodMA=0;// MODE_SMA 0 Simple moving average; MODE_EMA 1 
extern double Ecart=0.0028;
extern double EcartStop=0.0040; 
extern int    LenghtEcartStop=15;    
extern int    LenghtEcart=10;                        
extern int    MA_Shift=0; 
extern double Deviation=0.75;
extern bool Envelope = true;//if true will bild Enveloped else BBands
static string short_name;
//---- buffers Lines----------------------------------------------------
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
double MovingBufferMovingAverage[]; 
int    ExtCountedBars=0; 
string GlobalPrefix;
//-------------------------------------------------------------------------
int init()
{
   GlobalPrefix = Symbol() + Period();
   
   string short_name;   
   /*RefreshRates();/*When expert advisor has been calculating for 
   a long time and needs data refreshing*/   
   IndicatorBuffers(4);//Allocates memory for buffers used for custom indicator calculations
   /*
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)); 
   */
   SetIndexStyle(0,DRAW_LINE); 
   SetIndexBuffer(0,MovingBuffer);
   SetIndexStyle(1,DRAW_LINE);  
   SetIndexBuffer(1,UpperBuffer);
   SetIndexStyle(2,DRAW_LINE); 
   SetIndexBuffer(2,LowerBuffer);   
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,MovingBufferMovingAverage); 
   
   IndicatorDigits(Digits+1);// +n Ajouter number required  
   /*
   SetIndexDrawBegin(0,MovingBuffer);/*Sets the bar number (from the data
   beginning) from which the drawing of given indicator line must start)*/
   /*
   SetIndexDrawBegin(0,UpperBuffer);
   SetIndexDrawBegin(0,LowerBuffer); 
   SetIndexDrawBegin(0,MovingBufferMovingAverage);    
   */  
   
   short_name="Spread & BANDS ("+BandsPeriod+")";   
   IndicatorShortName(short_name);
   
   SetIndexLabel(0,short_name);
   //Sets drawing line desc 4 showing in the DataWindow n in tooltip
   SetIndexLabel(1,"Upper Band"); 
   SetIndexLabel(2,"Lower Band"); 
   SetIndexLabel(3,"Moving Average");
  
  
   if(Deviation<0.1) Deviation=0.1;
   if(Deviation>100.0) Deviation=100.0; 
  
   //----Will Show Info on Indicator-----------
   CreateInfoObjects();//added
   //------------------------------------------         
   return(0);
}
//----------------------------------------------------------------------
int start() 
{
//======Type your name==================================================
      /* 
      double currentMA;
      int j;      
      currentMA = iMA(0,PERIOD_H1,20,0,MODE_SMA,PRICE_TYPICAL,j);
      
      ObjectCreate("maValue",OBJ_LABEL,0,Time[j],currentMA);
      ObjectSetText("maValue",currentMA,8,"Tahoma",Gold);
      
      currentMA = iMA(0,PERIOD_H1,20,0,MODE_SMA,PRICE_TYPICAL,i);
      ObjectCreate("maValue"+i,OBJ_LABEL,0,Time[i],currentMA+0.0005);
      ObjectSetText("maValue"+i,currentMA,8,"Tahoma",Gold);
      */ 
//======================================================================     
   int counted_bars=IndicatorCounted();
   int i;
   int limit;    
   
   double deviation;
   double sum,oldval,newres;
//---- check for possible errors
   if(counted_bars<0) return(-1);         
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;   
//---- main loop      
   for(i=0; i<limit; i++)     
   {    
     if (iClose(SynbolCorr,0,i) != 0)
     {
        MovingBuffer[i]=MathLog(iClose(Synbol,0,i)/iClose(SynbolCorr,0,i));
//----Text about levels -------------------     
        if (WindowFind("Spread & BANDS ("+BandsPeriod+")") > 0)
        { 
            ObjectCreate("SPREADL", OBJ_LABEL, 0, 0, 0, 0, 0);  
            ObjectSet("SPREADL", OBJPROP_CORNER, 1);
            ObjectSet("SPREADL", OBJPROP_XDISTANCE, 90);
            ObjectSet("SPREADL", OBJPROP_YDISTANCE,120);   
            ObjectSetText("SPREADL", "           Spread:  "+MovingBuffer[0], 9, "Arial Black", SteelBlue);
        
            ObjectCreate("SpreadFirst", OBJ_LABEL, 0, 0, 0, 0, 0);
            ObjectSet("SpreadFirst", OBJPROP_CORNER, 3);
            ObjectSet("SpreadFirst", OBJPROP_XDISTANCE, 70); 
            ObjectSet("SpreadFirst", OBJPROP_YDISTANCE,10);  
            ObjectSetText("SpreadFirst", "Spread Value = "+" "+MovingBuffer[0], 10, "Arial Black", SteelBlue);      
     
            ObjectCreate("SpreadSecond", OBJ_LABEL, 1, 0, 0, 0, 0);
            ObjectSet("SpreadSecond", OBJPROP_CORNER, 3);
            ObjectSet("SpreadSecond", OBJPROP_XDISTANCE, 550); 
            ObjectSet("SpreadSecond", OBJPROP_YDISTANCE,10); 
            ObjectSetText("SpreadSecond", "Spread Value= "+"  "+MovingBuffer[0], 10, "Arial Black", SteelBlue);       
      
            ObjectCreate("PairsIndic", OBJ_LABEL, 1, 0, 0, 0, 0); 
            ObjectSet("PairsIndic", OBJPROP_CORNER, 4);
            ObjectSet("PairsIndic", OBJPROP_XDISTANCE, 335);  
            ObjectSet("PairsIndic", OBJPROP_YDISTANCE,10); 
            ObjectSetText("PairsIndic", "Pairs: "+Synbol+" - " +SynbolCorr , 10, "Arial Black", Crimson);
      
            ObjectCreate("StrategyIndic", OBJ_LABEL, 1, 0, 0, 0, 0);
            ObjectSet("StrategyIndic", OBJPROP_CORNER, 4);
            ObjectSet("StrategyIndic", OBJPROP_XDISTANCE, 512);  
            ObjectSet("StrategyIndic", OBJPROP_YDISTANCE,10); 
            ObjectSetText("StrategyIndic", " --- CONVERGENCY", 10, "Arial Black", Crimson); 

      
           //ObjectSetText("PairsCorrel", "Pairs:  "+Synbol+" - " +SynbolCorr +"  "+"YellowCapital", Xfont+1, "Arial Black", Crimson);    
    
           //----Comment seront HAUT Gauche -------------------------------------------------     
   
            //double FreeMargin=AccountFreeMargin(); 
            Comment(""
            ,"\n",      "www.yellowfinancial.com"//,DoubleToStr(FreeMargin,2) 
            ,"\n",      ""
            ,"\n",      "yellowfinancial@gmail.com"/*
            ,"\n",      ""
            ,"\n",      "Current time is of System     ",TimeToStr(TimeCurrent())
            ,"\n",      ""
            ,"\n",      "How are you Brother ",MovingBuffer[i] */
            ,"\n",      ""
            ,"\n",      "Time: ",TimeToStr(TimeCurrent(),TIME_SECONDS));   
        }        
     }
   }
//--------------------Envelop Bands---------------------------------------------- 
   //for(i=0; i<limit; i++)// not Bars     
   for(i=limit; i>=0; i--)// not Bars    
   {    
      MovingBufferMovingAverage[i] = iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);    
      if (WindowFind("Spread & BANDS ("+BandsPeriod+")") > 0)
      {
            ObjectCreate("MOVINGAVERAGE", OBJ_LABEL, 0, 0, 0, 0, 0); 
            ObjectSet("MOVINGAVERAGE", OBJPROP_CORNER, 1);
            ObjectSet("MOVINGAVERAGE", OBJPROP_XDISTANCE, 90);
            ObjectSet("MOVINGAVERAGE", OBJPROP_YDISTANCE,105);     
            ObjectSetText("MOVINGAVERAGE", "                         Moving Average:  "+MovingBufferMovingAverage[0], 9, "Arial Black", MediumPurple); 

            ObjectCreate("SPREADIFF", OBJ_LABEL, 0, 0, 0, 0, 0);  
            ObjectSet("SPREADIFF", OBJPROP_CORNER, 1);
            ObjectSet("SPREADIFF", OBJPROP_XDISTANCE, 90); 
            ObjectSet("SPREADIFF", OBJPROP_YDISTANCE,135);   
            ObjectSetText("SPREADIFF", "           DIfference:  "+(MovingBuffer[0]-MovingBufferMovingAverage[0]), 9, "Arial Black", Snow);

            ObjectCreate("ECARTHIGH", OBJ_LABEL, 0, 0, 0, 0, 0);
            ObjectSet("ECARTHIGH", OBJPROP_CORNER, 1);
            ObjectSet("ECARTHIGH", OBJPROP_XDISTANCE, 348);
            ObjectSet("ECARTHIGH", OBJPROP_YDISTANCE,105); 
            if (GlobalVariableCheck(GlobalPrefix + "initEcartStopUP") && GlobalVariableGet(GlobalPrefix + "initEcartStopUP") > 0)
            {
                ObjectSetText("ECARTHIGH", "Ecart High:  "+ GlobalVariableGet(GlobalPrefix + "initEcartStopUP") +"  ", 9, "Arial Black", Chocolate);
            }
            else
            {  
                ObjectSetText("ECARTHIGH", "Ecart High:  "+(MovingBufferMovingAverage[0]+Ecart)+"  ", 9, "Arial Black", Chocolate);     
            }

            ObjectCreate("ECARTLOW", OBJ_LABEL, 0, 0, 0, 0, 0);
            ObjectSet("ECARTLOW", OBJPROP_CORNER, 1);
            ObjectSet("ECARTLOW", OBJPROP_XDISTANCE, 348);  
            ObjectSet("ECARTLOW", OBJPROP_YDISTANCE,120);  
            if (GlobalVariableCheck(GlobalPrefix + "initEcartStopDN") && GlobalVariableGet(GlobalPrefix + "initEcartStopDN") > 0)
            {
                ObjectSetText("ECARTLOW", "Ecart Low:   " + GlobalVariableGet(GlobalPrefix + "initEcartStopDN") + "  ", 9, "Arial Black", NavajoWhite); 
            }
            else
            {
                ObjectSetText("ECARTLOW", "Ecart Low:   "+(MovingBufferMovingAverage[0]-Ecart)+"  ", 9, "Arial Black", NavajoWhite);    
            }
      }
      
      if(Envelope) 
      {   
         UpperBuffer[i] = (1+Deviation/100)*iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);
         LowerBuffer[i] = (1-Deviation/100)*iMAOnArray(MovingBuffer,0,BandsPeriod,MA_Shift,MethodMA,i);
      }
      else
      {
      // Bollinger Bands calculations from Custom Indicators (Bands.mq4)
         sum=0.0;
         int k=i+BandsPeriod-1;
         oldval=MovingBufferMovingAverage[i];
         while(k>=i)
           {
            newres=MovingBufferMovingAverage[k]-oldval;
            sum+=newres*newres;
            k--;
           }
         deviation=Deviation*MathSqrt(sum/BandsPeriod);
         UpperBuffer[i]=oldval+deviation;
         LowerBuffer[i]=oldval-deviation;
      // Bollinger Bands calculations from Custom Indicators (Bands.mq4)
      }               
    }  
   /*
   LowerBuffer[i] =               iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_LOWER,i);
   UpperBuffer[i] =               iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_UPPER,i); 
   MovingBufferMovingAverage[i] = iBandsOnArray(MovingBuffer,0,BandsPeriod,BandsDeviations,MA_Shift,MODE_MAIN,i);    
   */
   
   /*
   int   currentPeak=3.2;
   int   lastPeak=2.5;  
   DrawIndicatorTrendLine(Time[i], Time[i-1],MovingBufferMovingAverage[currentPeak],
         MovingBufferMovingAverage[lastPeak], Red, STYLE_DOT);
   */
  
//---------------------------------------------------------------------    
   /*
   Print("Account equity = ",AccountEquity());
   Print("Account free margin is ETSTTETTST ", AccountFreeMargin());
   */  
//---------------------------------------------------------------------      
   //----Incorrect-------------------
   /*
   ObjectCreate("Bar1", OBJ_LABEL, 0, 0, 0, 0, 0);
   ObjectSet("Bar1", OBJPROP_CORNER, 4);
   ObjectSet("Bar1", OBJPROP_XDISTANCE, 20);
   ObjectSet("Bar1", OBJPROP_YDISTANCE,30); 
   ObjectSetText("Bar1", "Philippe Dadjo = "+iClose(Synbol,0,i), 10, "Arial Black", Green);
   */
             
//==================================SELL LEVEL===================================    
   if (WindowFind("Spread & BANDS ("+BandsPeriod+")") > 0)
   {        
        double      P1;                                
        P1 = MovingBufferMovingAverage[0]+Ecart;   
  
        if (ObjectFind("label") == 1)
        {
            ObjectSet("label", OBJPROP_PRICE1, P1);
            ObjectSet("label", OBJPROP_PRICE2, P1);
            ObjectSet("label", OBJPROP_TIME1, Time[0]);
            ObjectSet("label", OBJPROP_TIME2, Time[LenghtEcart]);
        }
        else
        {
                 ObjectCreate("label", OBJ_TREND, 1, Time[0], P1, Time[LenghtEcart], P1);   
                 ObjectSet("label", OBJPROP_RAY, 0);
                 ObjectSet("label", OBJPROP_COLOR, Gold);
                 ObjectSet("label", OBJPROP_STYLE, STYLE_DOT);  
        }
        //==================================BUY LEVEL===================================   
           double      Pone;
           Pone = MovingBufferMovingAverage[0]-Ecart;    
   
  
        if (ObjectFind("label2") == 1)
        {
            ObjectSet("label2", OBJPROP_PRICE1, Pone);
            ObjectSet("label2", OBJPROP_PRICE2, Pone);
            ObjectSet("label2", OBJPROP_TIME1, Time[0]);
            ObjectSet("label2", OBJPROP_TIME2, Time[LenghtEcart]);
        }
        else
        {
            ObjectCreate("label2", OBJ_TREND, 1, Time[0], Pone, Time[LenghtEcart], Pone);  
            ObjectSet("label2", OBJPROP_RAY, 0);
            ObjectSet("label2", OBJPROP_COLOR, Gold);
            ObjectSet("label2", OBJPROP_STYLE, STYLE_DOT);  
        }
    //=========================STOP HIGH====================================   
       double      P1Stop;                                
       P1Stop = MovingBufferMovingAverage[0]+EcartStop;     
     
       if (ObjectFind("labelHigh") == 1)
       {
            if (GlobalVariableCheck(GlobalPrefix + "initEcartStopUP") && GlobalVariableGet(GlobalPrefix + "initEcartStopUP") > 0)
            {
                ObjectSet("labelHigh", OBJPROP_PRICE1, GlobalVariableGet(GlobalPrefix + "initEcartStopUP"));
                ObjectSet("labelHigh", OBJPROP_PRICE2, GlobalVariableGet(GlobalPrefix + "initEcartStopUP"));
            }
            else
            {  
                ObjectSet("labelHigh", OBJPROP_PRICE1, P1Stop);
                ObjectSet("labelHigh", OBJPROP_PRICE2, P1Stop);     
            }
           
           ObjectSet("labelHigh", OBJPROP_TIME1, Time[0]);
           ObjectSet("labelHigh", OBJPROP_TIME2, Time[LenghtEcartStop]);
       }
       else
       {
           ObjectCreate("labelHigh", OBJ_TREND, 1, Time[0], P1Stop, Time[LenghtEcartStop], P1Stop);   
           ObjectSet("labelHigh", OBJPROP_RAY, 0);
           ObjectSet("labelHigh", OBJPROP_COLOR, Red);
           ObjectSet("labelHigh", OBJPROP_STYLE, STYLE_DASH);   
       }
    //==========================STOP LOW===================================      
       double PoneStop;
       PoneStop = MovingBufferMovingAverage[0]-EcartStop;    
   
  
       if (ObjectFind("labelStop") == 1)
       {
            if (GlobalVariableCheck(GlobalPrefix + "initEcartStopDN") && GlobalVariableGet(GlobalPrefix + "initEcartStopDN") > 0)
            {
                ObjectSet("labelStop", OBJPROP_PRICE1, GlobalVariableGet(GlobalPrefix + "initEcartStopDN"));
                ObjectSet("labelStop", OBJPROP_PRICE2, GlobalVariableGet(GlobalPrefix + "initEcartStopDN"));                
            }
            else
            {
                ObjectSet("labelStop", OBJPROP_PRICE1, PoneStop);
                ObjectSet("labelStop", OBJPROP_PRICE2, PoneStop);    
            }
           
           ObjectSet("labelStop", OBJPROP_TIME1, Time[0]);
           ObjectSet("labelStop", OBJPROP_TIME2, Time[LenghtEcartStop]);
       }
       else
       {
           if (WindowsTotal() > 1)
           {
               ObjectCreate("labelStop", OBJ_TREND, 1, Time[0], PoneStop, Time[LenghtEcart], PoneStop);  
               ObjectSet("labelStop", OBJPROP_RAY, 0);
               ObjectSet("labelStop", OBJPROP_COLOR, Red);
               ObjectSet("labelStop", OBJPROP_STYLE, STYLE_DASH); 
           } 
       }   
  }             
  return(0);   
}  
//=================After Being Initialized==============================
void CreateInfoObjects()
{       
    double iecart,iecartstop;
    iecart = Ecart*10000;
    iecartstop=EcartStop*10000; 
    int NumRound = 3;
    int Xdist = 90; 
    int Xfont = 9; 
    int Ydist = 60;  
    if (WindowFind("Spread & BANDS ("+BandsPeriod+")") > 0)
    {
        ObjectCreate("TIME", OBJ_LABEL, 0, 0, 0, 0, 0);
        ObjectSet("TIME", OBJPROP_CORNER, 1);
        ObjectSet("TIME", OBJPROP_XDISTANCE, Xdist+11);
        ObjectSet("TIME", OBJPROP_YDISTANCE,Ydist-15);
        ObjectSetText("TIME", "Time:  "+TimeToStr(TimeCurrent(),TIME_SECONDS)+"  ", Xfont, "Arial Black", Snow);    

        ObjectCreate("ECART", OBJ_LABEL, 0, 0, 0, 0, 0);
        ObjectSet("ECART", OBJPROP_CORNER, 1);
        ObjectSet("ECART", OBJPROP_XDISTANCE, Xdist+23);
        ObjectSet("ECART", OBJPROP_YDISTANCE,Ydist);     
        ObjectSetText("ECART", "Ecart Indic:  "+DoubleToStr(iecart,NumRound)+"  ", Xfont, "Arial Black",BurlyWood );    

        ObjectCreate("ECARTSTOP", OBJ_LABEL, 0, 0, 0, 0, 0);
        ObjectSet("ECARTSTOP", OBJPROP_CORNER, 1);
        ObjectSet("ECARTSTOP", OBJPROP_XDISTANCE, Xdist+23);
        ObjectSet("ECARTSTOP", OBJPROP_YDISTANCE,Ydist+15);
        ObjectSetText("ECARTSTOP", "EcartStop Indic:  "+DoubleToStr(iecartstop,NumRound)+"  ", Xfont, "Arial Black", LightSalmon);    

        ObjectCreate("BANDSPERIOD", OBJ_LABEL, 0, 0, 0, 0, 0); 
        ObjectSet("BANDSPERIOD", OBJPROP_CORNER, 1);
        ObjectSet("BANDSPERIOD", OBJPROP_XDISTANCE, Xdist+23);
        ObjectSet("BANDSPERIOD", OBJPROP_YDISTANCE,Ydist+30);  
        ObjectSetText("BANDSPERIOD", "Bands Period:  "+BandsPeriod+"         ", Xfont, "Arial Black", LightSkyBlue);
    }
    /* 
    string EnvelopeMA, ExitOpposite;
    if(Envelope== true) EnvelopeMA= "ExitOnMA"; 
     else             EnvelopeMA= "ExitOpposite";
 
    ObjectCreate("RETURNTOMA", OBJ_LABEL, 0, 0, 0, 0, 0); 
    ObjectSet("RETURNTOMA", OBJPROP_CORNER, 1);
    ObjectSet("RETURNTOMA", OBJPROP_XDISTANCE, Xdist+235);
    ObjectSet("RETURNTOMA", OBJPROP_YDISTANCE,Ydist+45);  
    ObjectSetText("RETURNTOMA", "ReturnToMA: "+EnvelopeMA+"         ", Xfont, "Arial Black", Orchid);   
    */
} 
    
//=====================================================================
   /*
   DrawIndicatorTrendLine(Time[100], 
                                 Time[50], 
                                 BandsDeviations,
                                 BandsDeviations, 
                                 Green, STYLE_SOLID);   
   void DrawIndicatorTrendLine(datetime x1, datetime x2, double y1, 
                            double y2, color lineColor, double style)
   {
   int indicatorWindow = WindowFind(short_name);
   if(indicatorWindow < 0)
       return;
   string label = "MACD_DivergenceLine " + DoubleToStr(x1, 0);
   //ObjectDelete(label);
   ObjectCreate(label, OBJ_TREND, indicatorWindow, x1, y1, x2, y2, 
                0, 0);
   ObjectSet(label, OBJPROP_RAY, 0);
   ObjectSet(label, OBJPROP_COLOR, lineColor);
   ObjectSet(label, OBJPROP_STYLE, style); 
   }
   */                                  
//=====================================================================
/*
    Trend_Line(Time[10],Time[0],Open[10],Open[0],Gold,STYLE_SOLID);
    void Trend_Line(
    datetime x1, datetime x2, double y1, 
    double y2, color lineColor, double style)
    {
    //~~~~~~~~~~
    string label = "_Trend_Line_";
    ObjectDelete(label);
    ObjectCreate(label, OBJ_TREND, 1, x1, y1, x2, y2, 0, 0);
    ObjectSet(label, OBJPROP_RAY, 0);
    ObjectSet(label, OBJPROP_COLOR, lineColor);
    ObjectSet(label, OBJPROP_STYLE, style);
    //~~~~~~~~~~
    } 
*/   